<script setup>
import {
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  ArrowRight,
  Back
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { permissionSearch, userInfoService } from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import { useTokenStore } from '@/stores/token.js'
import { permissionStoreService } from '@/stores/permission.js'
import { setRoutes } from '@/router/index.js'
const permissionStore = permissionStoreService()
const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()
//用户名
const username = sessionStorage.getItem('user')
//调用函数,获取用户详细信息
const getUserInfo = async () => {
  //调用接口
  let result = await userInfoService(username)
  //数据存储到pinia中
  userInfoStore.setInfo(result.data)
  //获取权限信息，存储到pinia中
  let permissionsResult = await permissionSearch(result.data.username)
  const permission = permissionsResult.data
  permissionStore.setPermission(permission)
  setRoutes()
}
getUserInfo()

//条目被点击后,调用的函数
import { useRouter } from 'vue-router'
const router = useRouter()
import { ElMessage, ElMessageBox } from 'element-plus'
const handleCommand = command => {
  //判断指令
  if (command === 'logout') {
    //退出登录

    //1.弹出确认框
    ElMessageBox.confirm('您确认要退出吗?', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(async () => {
        //退出登录
        //2.清空pinia中存储的token以及个人信息
        tokenStore.removeToken()
        userInfoStore.removeInfo()
        permissionStore.removePermission()
        sessionStorage.clear()
        //3.跳转到登录页面
        await router.push('/index')
        ElMessage({
          type: 'success',
          message: '退出登录成功'
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了退出登录'
        })
      })
  } else {
    //路由
    router.push('/user/' + command)
  }
}
import { computed } from 'vue'
// 面包屑计算属性
const breadcrumbs = computed(() => {
  const matchedRoutes = router.currentRoute.value.matched
  const breadcrumbItems = []
  if (matchedRoutes) {
    // 添加主页
    breadcrumbItems.push({ text: '主页', to: '/' })
    breadcrumbItems.push({ text: '主菜单', to: '/layout' })
    matchedRoutes.forEach(route => {
      const title = route.meta.title || '' // 从 meta 中获取标题
      const path = route.path // 获取路径
      if (title) {
        breadcrumbItems.push({ text: title, to: path }) // 添加面包屑对象
      }
    })
  }
  return breadcrumbItems // 返回完整面包屑
})

let permissions = permissionStore.permission

function hasChildren(m) {
  return m.children && m.children.length > 0
}

function handleRouter(c) {
  router.push(c.path)
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
        active-text-color="#ffd04b"
        background-color="#232323"
        text-color="#fff"
        router
      >
        <template v-for="m in permissions" :key="m.path">
          <!--判断是否有子菜单-->
          <!--判断是否有子菜单-->
          <el-sub-menu v-if="hasChildren(m)" :index="m.path">
            <template #title>
              <el-icon><component :is="m.icon"></component></el-icon>
              <span>{{ m.comment }}</span>
            </template>
            <!--渲染子菜单-->
            <el-menu-item-group>
              <el-menu-item
                v-for="c in m.children"
                :key="c.path"
                :index="c.path"
                @click="handleRouter(c)"
              >
                <el-icon><component :is="c.icon"></component></el-icon>
                <span>{{ c.comment }}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
          <el-menu-item
            v-else-if="m.name !== 'Home'"
            :index="m.path"
            @click="handleRouter(m)"
          >
            <el-icon><component :is="m.icon"></component></el-icon>
            <span>{{ m.comment }}</span>
          </el-menu-item>
          <el-menu-item
            :index="'/index'"
            @click="handleRouter({ path: '/index' })"
            style="position: fixed; bottom: 0; left: 0; width: 10%"
          >
            <el-icon><Back /></el-icon>
            <span>返回首页</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <!-- 面包屑导航 -->
        <el-breadcrumb :separator-icon="ArrowRight">
          <el-breadcrumb-item
            v-for="(item, index) in breadcrumbs"
            :key="index"
            :to="item.to"
          >
            {{ item.text }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 面包屑导航结束 -->
        <div>
          <span
            style="
              margin-right: 20px;
              font-size: 16px;
              border-right: 2px solid #0070aa;
              padding-right: 10px;
            "
            >用户：<strong>{{ username }}</strong></span
          >
          <el-dropdown placement="bottom-end" @command="handleCommand">
            <span class="el-dropdown__box">
              <el-avatar
                :src="
                  userInfoStore.info.userPic
                    ? userInfoStore.info.userPic
                    : avatar
                "
              />
              <el-icon>
                <CaretBottom />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="UserInfo" :icon="User"
                  >基本资料</el-dropdown-item
                >
                <el-dropdown-item command="UserAvatar" :icon="Crop"
                  >更换头像</el-dropdown-item
                >
                <el-dropdown-item command="UserResetPassword" :icon="EditPen"
                  >重置密码</el-dropdown-item
                >
                <el-dropdown-item command="logout" :icon="SwitchButton"
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>管理平台 ©2024 Created by seventeen</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-image: linear-gradient(
      -225deg,
      #473b7b 0%,
      #3584a7 51%,
      #30d2be 100%
    );

    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      background-color: transparent;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
// 面包屑导航的样式
.el-breadcrumb {
  font-size: 20px; // 假设代码中的字体大小是14px，你可以根据实际情况调整这个值
  color: #666; // 可以根据需要调整颜色
}
</style>
